package appCommand

import (
	"fmt"
	"tushare/service/ssh"
)

type AppManageShell struct{}

func (ams AppManageShell) ManageApp(appName, command string) {
	if appName == "nginx" && command == "start" {
		ams.StartNginx("")
	} else if appName == "kafka" && command == "start" {
		ams.StartKafka("120.26.76.223")
	}
}

func (ams AppManageShell) StartNginx(ip string) {
	str := ""
	str = fmt.Sprintf("%s%s", str, "nohup /usr/local/nginx/sbin/nginx & >> /usr/local/nginx/logs/nginx_run.log;")

	sh := new(ssh.Ssh)
	sh.LoginEcs(ip, str)
}

func (ams AppManageShell) StopNginx(ip string) {
	sh := new(ssh.Ssh)
	res := sh.LoginEcs(ip, "ps -ef | grep /usr/local/nginx/sbin/nginx | grep -v grep | awk '{print$2}'")
	sh.LoginEcs(ip, fmt.Sprintf("kill -QUIT %s", res))
}

func (ams AppManageShell) ReloadNginx(ip string) {
	sh := new(ssh.Ssh)
	sh.LoginEcs(ip, "/usr/local/nginx/sbin/nginx -s reload")
}

func (ams AppManageShell) StartRedis(ip string) {
	sh := new(ssh.Ssh)
	go sh.LoginEcs(ip, "nohup /usr/local/redis-6.2.6/src/redis-server /usr/local/redis-6.2.6/redis.conf & >> /usr/local/redis-6.2.6/redis.log")
}

func (ams AppManageShell) StopRedis(ip string) {
	sh := new(ssh.Ssh)
	res := sh.LoginEcs(ip, "ps -ef | grep /usr/local/redis-6.2.6/src/redis-server | grep -v grep | awk '{print$2}'")
	sh.LoginEcs(ip, fmt.Sprintf("kill -9 %s", res))
}

func (ams AppManageShell) StartKafka(ip string) {

}

//获取内存总大小
func (ams AppManageShell) MemTotal(ip string) {
	sh := new(ssh.Ssh)
	sh.LoginEcs(ip, "cat /proc/meminfo | awk '$1 ~/MemTotal/' | awk '{print $2}'")
}
